package com.example.AIstudy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.AIstudy.dao.LevelRecordDao;
import com.example.AIstudy.dao.UserDao;
import com.example.AIstudy.dao.UserSkillScoreDao;
import com.example.AIstudy.domain.UserSkillScore;
import com.example.AIstudy.exception.AIstudyException;
import com.example.AIstudy.service.UserSkillScoreService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;

@Service
public class UserScoreSkillServiceImpl extends ServiceImpl<UserSkillScoreDao, UserSkillScore> implements UserSkillScoreService {

    @Autowired
    UserDao userDao;

    @Autowired
    UserSkillScoreDao userSkillScoreDao;

    @Autowired
    LevelRecordDao levelRecordDao;

    public void updateUserSkill() throws AIstudyException{
        try{
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MONTH, -1); // 回退一个月
            // 设置为零点
            cal.set(Calendar.HOUR_OF_DAY, 0);
            // 设置为零分
            cal.set(Calendar.MINUTE, 0);
            // 设置为零秒
            cal.set(Calendar.SECOND, 0);
            // 设置为零毫秒
            cal.set(Calendar.MILLISECOND, 0);
            //获取到一个月前的时间戳
            long timestamp = cal.getTimeInMillis();
            //获取到当前的时间戳
            long timestampNow = System.currentTimeMillis();
            List<Integer> stuIDs=userDao.getAllUserID();
            int size=stuIDs.size();
            UserSkillScore userSkillScore;
            for(int i=0;i<size;i++){
                Integer stuID=stuIDs.get(i);
                //先把原先的记录删除
                userSkillScoreDao.deleteByStuID(stuID);
                userSkillScore=new UserSkillScore();
                userSkillScore.setStuID(stuID);
                //获取到各个题型的做题数量
                List<Integer>cuotiNum=userSkillScoreDao.getCuotiNumByCaategory(stuID, new Timestamp(timestamp), new Timestamp(timestampNow));
                userSkillScore.setCuotiFanyiNum(cuotiNum.get(0));
                userSkillScore.setCuotiXuanzeNum(cuotiNum.get(1));
                userSkillScore.setCuotiYueduNum(cuotiNum.get(2));
                userSkillScore.setCuotiZuowenNum(cuotiNum.get(3));
                Float totalNum= (float) (cuotiNum.get(0)+cuotiNum.get(1)+cuotiNum.get(2)+cuotiNum.get(3));
                //获取各个题型的得分率
                List<Float>redoScore=levelRecordDao.getCorrectRateInRedo(stuID, new Timestamp(timestamp), new Timestamp(timestampNow));
                List<Float>exeScore=levelRecordDao.getCorrectRateInExe(stuID, new Timestamp(timestamp), new Timestamp(timestampNow));
                //计算各个题型的失分率，如果没有练习过，失分率为1，说明更要推荐题给用户练习
                userSkillScore.setFanyiScoreRate(1-(redoScore.get(0)+exeScore.get(0))/2);
                userSkillScore.setXuanzeScoreRate(1-(redoScore.get(1)+exeScore.get(1))/2);
                userSkillScore.setYueduScoreRate(1-(redoScore.get(2)+exeScore.get(2))/2);
                userSkillScore.setZuowenScoreRate(1-(redoScore.get(3)+exeScore.get(3))/2);
                //计算每个用户的薄弱点指数
                if(totalNum==0)totalNum= 1.0F;
                userSkillScore.setFanyiSkillScore((float) (cuotiNum.get(0)/totalNum*0.5+userSkillScore.getFanyiScoreRate()*0.5));
                userSkillScore.setXuanzeSkillScore((float) (cuotiNum.get(1)/totalNum*0.5+userSkillScore.getXuanzeScoreRate()*0.5));
                userSkillScore.setYueduSkillScore((float) (cuotiNum.get(2)/totalNum*0.5+userSkillScore.getYueduScoreRate()*0.5));
                userSkillScore.setZuowenSkillScore((float) (cuotiNum.get(3)/totalNum*0.5+userSkillScore.getZuowenScoreRate()*0.5));
                userSkillScoreDao.insert(userSkillScore);
            }

        }catch (Exception e){
            throw new AIstudyException(AIstudyException.ErrorType.UPDATE_ERROR,"更新用户的薄弱点信息失败");
        }
    }

    //更新用户的信息，这个可以在插入数据的时候调用
    public void updateUserSkillByStuID(Integer stuID) throws AIstudyException{
        try{
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MONTH, -1); // 回退一个月
            // 设置为零点
            cal.set(Calendar.HOUR_OF_DAY, 0);
            // 设置为零分
            cal.set(Calendar.MINUTE, 0);
            // 设置为零秒
            cal.set(Calendar.SECOND, 0);
            // 设置为零毫秒
            cal.set(Calendar.MILLISECOND, 0);
            //获取到一个月前的时间戳
            long timestamp = cal.getTimeInMillis();
            //获取到当前的时间戳
            long timestampNow = System.currentTimeMillis();
            UserSkillScore userSkillScore;
            userSkillScore=new UserSkillScore();
            userSkillScore.setStuID(stuID);
            //获取到各个题型的做题数量
            List<Integer>cuotiNum=userSkillScoreDao.getCuotiNumByCaategory(stuID, new Timestamp(timestamp), new Timestamp(timestampNow));
            userSkillScore.setCuotiFanyiNum(cuotiNum.get(0));
            userSkillScore.setCuotiXuanzeNum(cuotiNum.get(1));
            userSkillScore.setCuotiYueduNum(cuotiNum.get(2));
            userSkillScore.setCuotiZuowenNum(cuotiNum.get(3));
            Float totalNum= (float) (cuotiNum.get(0)+cuotiNum.get(1)+cuotiNum.get(2)+cuotiNum.get(3));
            //获取各个题型的得分率
            List<Float>redoScore=levelRecordDao.getCorrectRateInRedo(stuID, new Timestamp(timestamp), new Timestamp(timestampNow));
            List<Float>exeScore=levelRecordDao.getCorrectRateInExe(stuID, new Timestamp(timestamp), new Timestamp(timestampNow));
            //计算各个题型的失分率，如果没有练习过，失分率为1，说明更要推荐题给用户练习
            userSkillScore.setFanyiScoreRate(1-(redoScore.get(0)+exeScore.get(0))/2);
            userSkillScore.setXuanzeScoreRate(1-(redoScore.get(1)+exeScore.get(1))/2);
            userSkillScore.setYueduScoreRate(1-(redoScore.get(2)+exeScore.get(2))/2);
            userSkillScore.setZuowenScoreRate(1-(redoScore.get(3)+exeScore.get(3))/2);
            //计算每个用户的薄弱点指数
            if(totalNum==0)totalNum= 1.0F;
            userSkillScore.setFanyiSkillScore((float) (cuotiNum.get(0)/totalNum*0.5+userSkillScore.getFanyiScoreRate()*0.5));
            userSkillScore.setXuanzeSkillScore((float) (cuotiNum.get(1)/totalNum*0.5+userSkillScore.getXuanzeScoreRate()*0.5));
            userSkillScore.setYueduSkillScore((float) (cuotiNum.get(2)/totalNum*0.5+userSkillScore.getYueduScoreRate()*0.5));
            userSkillScore.setZuowenSkillScore((float) (cuotiNum.get(3)/totalNum*0.5+userSkillScore.getZuowenScoreRate()*0.5));
            //删除用户的记录
            userSkillScoreDao.deleteByStuID(stuID);
            //重新插入用户的记录
            userSkillScoreDao.insert(userSkillScore);
        }catch (Exception e){
            throw new AIstudyException(AIstudyException.ErrorType.UPDATE_ERROR,e.getMessage()+"更新用户的薄弱点信息失败");
        }
    }
}
